using Domain.Dto;
using Gmmy.DomainExtension;
using Gmmy.Utility;
using System;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;

namespace Domain.Model
{
    /// <summary>
    /// 功能模块API模型
    /// </summary>
    public class ModuleAction : EntityBase
    {
		#region 领域对象属性
        /// <summary>
        /// 编号
        /// </summary>
        [DisplayName(@"编号")]
        [Key]
        public Guid ID { get; set; }

        /// <summary>
        /// 模块编号
        /// </summary>
        [DisplayName(@"模块编号")]
        public Guid ModuleId { get; set; }

        /// <summary>
        /// 功能名
        /// </summary>
        [DisplayName(@"功能名")]
        public string ActionName { get; set; }

        /// <summary>
        /// Api地址
        /// </summary>
        [DisplayName(@"Api地址")]
        public string ActionAddress { get; set; }

        /// <summary>
        /// 排序
        /// </summary>
        [DisplayName(@"排序")]
        public int Sort { get; set; }

        /// <summary>
        /// 创建时间
        /// </summary>
        [DisplayName(@"创建时间")]
        public DateTime CreateTime { get; set; }

        /// <summary>
        /// 逻辑删除
        /// </summary>
        [DisplayName(@"逻辑删除")]
        public bool IsDeleted { get; set; }


        #endregion

		#region 领域对象行为
		public ModuleAction() { }//空构造函数

        public ModuleAction(ModuleActionCreateInput input)
        {
            if (string.IsNullOrWhiteSpace(input.Name))
            {
                throw new CustomException("请输入模块名称");
            }
            if (string.IsNullOrWhiteSpace(input.ApiAddress) || !input.ApiAddress.StartsWith("api/"))
            {
                throw new CustomException("api地址必须以\"api/\"开头!");
            }
            if (input.Sort < 0)
            {
                throw new CustomException("排序不能小于0");
            }
            ID = Guid.NewGuid();
            ActionName = input.Name;
            ModuleId = input.ModuleId;
            ActionAddress = input.ApiAddress;
            Sort = input.Sort ?? 0;
            CreateTime=DateTime.Now;
            IsDeleted = false;
        }

        public void Update(ModuleActionUpdateInput input)
        {
            if (string.IsNullOrWhiteSpace(input.Name))
            {
                throw new CustomException("请输入模块名称");
            }
            if (string.IsNullOrWhiteSpace(input.ApiAddress) || !input.ApiAddress.StartsWith("api/"))
            {
                throw new CustomException("api地址必须以\"api/\"开头!");
            }
            if (input.Sort < 0)
            {
                throw new CustomException("排序不能小于0");
            }
            ActionName = input.Name;
            ActionAddress = input.ApiAddress;
            Sort = input.Sort ?? 0;
        }
        #endregion

    }
}